/**
* Created by super on 2016/6/7.
*/



function getCsrf(){
    return $("#csrfmiddletocken input[name='csrfmiddlewaretoken']").val();
    }

//点赞操作
function article_thumbup(ths,article_id, comment_type,parent_id, textobj) {
    //获取当前登录的状态，如果未登录不允许操作
    var login_status = $("#loginstatus").text();
    //如果是发表评论获取评论内容
    if (textobj.length == 0){
        textobj = "saytext"
    }
    console.log(textobj);
    var comment=$("#"+textobj).val();
    console.log(comment);
    if (login_status == 1){
        //已登录
        csrftocker=getCsrf();
        console.log('article_id',article_id);
        console.log(csrftocker);
        //异步提交点赞或评论
        $.post("/bbs/comment/",
            {
                'comment_type':comment_type,
                'article_id':article_id,
                'csrfmiddlewaretoken':csrftocker,
                'parent_comment_id':parent_id,
                'saytext':comment
            },
            function (return_data) {
                console.log('post_ok',return_data);
                if (return_data == 'ok'){
                    if (comment_type == "2"){ //如果点赞就让点赞按钮值+1
                    var curr_value = parseInt($("#thumbup span").text());
                    $("#thumbup span").text(curr_value + 1);
                        //评论树点击顶
                        if ($(ths).hasClass('p4')){
                            console.log('select');
                            var dingvalue = $(ths).find("label").text();
                            $(ths).find("label").text(parseInt(dingvalue)+1);
                        }
                    }else{
                        //发表评论，当前页刷新一下
                        window.location.reload();
                    }
                }
            }
            
        );
    }else{
        //未登录
        //console.log(login_status);
        $('#thumbup').popover('show');
        return false
    }
}


$(document).ready(function(){
    $(".comment-box button").click(function(){
        var comment_text = $(".comment-box textarea").val();
        if (comment_text.trim().length <5){
            alert("评论不能少于5个字sb");
        }else{
            //post

            $.post("{% url 'post_comment' %}",
                    {
                        'comment_type':1,
                        article_id:"{{ article_obj.id }}",
                        parent_comment_id:null,
                        'comment':comment_text.trim(),
                        'csrfmiddlewaretoken':getCsrf()
                    },//end post args
                    function(callback){
                        console.log(callback);
            })//end post
        }
    });//end button click
});

function clear_alert(obj) {
    $("#"+obj).css({'border-color':''})
}

function show_alert(formobj, alertobj) {
    $("#"+formobj).css({'border-color': 'red'});
    $("#"+alertobj).text('1-20个字符以内');
    $("#"+alertobj).removeAttr('hidden');

}

function post_article(type) {
    var title = $("#id_title").val();
    var brief = $("#id_brief").val();
    var category = $("#id_category").val();
   
    editor.sync();
    var content = $("#article-content").val();

    if (title.length > 20 || title.length == 0){
        show_alert("id_title","alert_title");
        window.setTimeout("$('#alert_title').attr('hidden',true);",3000);
        return false
    }else{
        clear_alert("id_title");
        
    }

    if (brief.length > 20 || brief.length == 0){
        show_alert("id_brief","alert_brief");
        window.setTimeout("$('#alert_brief').attr('hidden',true);",3000);
        return false
    }else{
        clear_alert("id_brief");
    }
    
    $("#status").val(type);
    console.log(type);
    $("#frm_article").submit();
/*
    $.post("{% url 'pub_article'  %}",
        {
            status:type,
            title:title,
            brief:brief,
            category:category,
            content:content,
            head_img:head_img
        }

    )*/

}

function close_common(ths) {
    $(ths).parent().parent().remove();
}

//评论数中点击回复自动增加一个回复框
function tree_common(ths,article_id, comment_type, comment_id){
    var insert_li ="<li class='reply-comment'><textarea style='width: 591px; height: 30px;' id='saytext_"+comment_id+"'>";
    insert_li += "</textarea><span><a class='btn btn-success' style='padding-top:0; height:25px;'";
    insert_li += "onclick='article_thumbup(this," + article_id +","+ comment_type+","+ comment_id +",\"saytext_"+comment_id+"\")'>发表</a></span>";
    insert_li += "<span><a onclick='close_common(this);' class='p5' ><i class='glyphicon glyphicon-open'></i>收起</a><span></li>";
    if (!$(ths).parent().parent().next().first().hasClass('reply-comment')) {
        $(ths).parent().parent().after(insert_li)
    }
}